/**
 * 
 */
package com.${ltdAbbr}.xiao.${module}.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.${ltdAbbr}.xiao.${module}.dal.pojo.${table.name?cap_first}Do;
import com.${ltdAbbr}.xiao.${module}.dal.service.${table.name?cap_first}Service;
import com.yt.xiao.commons.pojo.QueryPageParam;
import com.yt.xiao.commons.pojo.QueryResult;
import com.yt.xiao.commons.pojo.UpdateResult;
import com.yt.xiao.commons.util.XYValidator;
import com.yt.xiao.commons.util.XYValidator.CodeEntity;
import com.yt.xiao.core.annotation.XYPermission;
import com.yt.xiao.database.dao.XYAbstractDao;


/**
 * ${table.comment}dal service
 * 
 * @author ${author}
 * @version ${curTime}
 */
@Controller
@RequestMapping("/main/${table.name?uncap_first}")
public class ${table.name?cap_first}Controller {
    
    private static final Logger logger = LoggerFactory.getLogger(${table.name?cap_first}Controller.class);

    @Autowired
    @Qualifier(value="${module}_${table.name?uncap_first}DalService")
    private ${table.name?cap_first}Service ${table.name?uncap_first}Service;


    /**
     *  ${table.comment}  管理页面
     * @return mav
     */
    @RequestMapping("/manage${actionSuffix}")
    @XYPermission("${module}:${table.name?uncap_first}:manage")
    public ModelAndView manage() {
        ModelAndView mav = new ModelAndView("tpl/main/${table.lowerName}_manage");
        return mav;
    }

    /**
     * 分页查询 ${table.comment}  列表
     * @param ${table.name?uncap_first}
     * @param queryPageParam
     * @return ${table.comment} list
     */
    @ResponseBody
    @RequestMapping("/list${actionSuffix}")
    @XYPermission("${module}:${table.name?uncap_first}:list")
    public QueryResult<${table.name?cap_first}Do> list(${table.name?cap_first}Do ${table.name?uncap_first}, QueryPageParam queryPageParam){
        return ${table.name?uncap_first}Service.selectList(${table.name?uncap_first}, queryPageParam);
    }

    /**
     * 保存 ${table.comment}
     * @param ${table.name?uncap_first}
     * @return
     */
    @ResponseBody
    @RequestMapping("/add${actionSuffix}")
    @XYPermission("${module}:${table.name?uncap_first}:add")
    public UpdateResult add(${table.name?cap_first}Do ${table.name?uncap_first}) {
        UpdateResult result;
        if(${table.name?uncap_first}==null) {
            result = new UpdateResult(-1, "${table.name?uncap_first} is null");
        } else {
            try {
                result = ${table.name?uncap_first}Service.save(${table.name?uncap_first});
            } catch(Exception e) {
                final String exceptionMessage = "保存${table.comment}失败,参考信息:"+e.getMessage();
                result = new UpdateResult(-500, exceptionMessage);
                logger.error(exceptionMessage, e);
            }
        }
        return result;
    }

    /**
     * 删除 ${table.comment}
     * @param ${table.name?uncap_first}
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete${actionSuffix}")
    @XYPermission("${module}:${table.name?uncap_first}:delete")
    public UpdateResult delete(${table.name?cap_first}Do ${table.name?uncap_first}) {
        UpdateResult result;
        if(${table.name?uncap_first}==null) {
            result = new UpdateResult(-1, "${table.name?uncap_first} is null");
        } else {
            try {
                result = ${table.name?uncap_first}Service.delete(${table.name?uncap_first});
            } catch(Exception e) {
                final String exceptionMessage = "删除${table.comment}失败,参考信息:"+e.getMessage();
                result = new UpdateResult(-500, exceptionMessage);
                logger.error(exceptionMessage, e);
            }
        }
        return result;
    }

    /**
     * 更新 ${table.comment}
     * @param ${table.name?uncap_first}
     * @return
     */
    @ResponseBody
    @RequestMapping("/edit${actionSuffix}")
    @XYPermission("${module}:${table.name?uncap_first}:edit")
    public UpdateResult edit(${table.name?cap_first}Do ${table.name?uncap_first}) {
        UpdateResult result;
        if(${table.name?uncap_first}==null) {
            result = new UpdateResult(-1, "${table.name?uncap_first} is null");
        } else {
            try {
                result = ${table.name?uncap_first}Service.update(${table.name?uncap_first});
            } catch(Exception e) {
                final String exceptionMessage = "更新${table.comment}失败,参考信息:"+e.getMessage();
                result = new UpdateResult(-500, exceptionMessage);
                logger.error(exceptionMessage, e);
            }
        }
        return result;
    }
}
